package com.example.fw;

import javax.swing.JDialog;

import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JLabelOperator;

public class HelperBase
{
	protected final ApplicationManager manager;
	protected final JFrameOperator mainFrame;

	public HelperBase(ApplicationManager applicationManager)
	{
		this.manager = applicationManager;
		this.mainFrame = manager.getApplication();
	}

	/* this method in the absence of dialogue freezes */
	protected String waitMessageDialog(String title, int timeout)
	{
		long start = System.currentTimeMillis();
		long currentTime = start;
		while (currentTime < start + timeout)
		{
			JDialog dialog = JDialogOperator.findJDialog(mainFrame.getOwner(), title, false, false);
			if (dialog != null)
			{
				JDialogOperator dialogOp = new JDialogOperator(dialog);
				String message = new JLabelOperator(dialogOp).getText();
				dialogOp.requestClose();
				return message;
			}

			try
			{
				Thread.sleep(500);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		return null;
	}

	protected String waitMessageDialogInSeconds(String title, int timeout)
	{
		for (int i = 0; i < timeout; i++)
		{
			JDialog dialog = JDialogOperator.findJDialog(mainFrame.getOwner(), title, false, false);
			if (dialog != null)
			{
				JDialogOperator dialogOp = new JDialogOperator(dialog);
				String message = new JLabelOperator(dialogOp).getText();
				dialogOp.requestClose();
				return message;
			}

			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		return null;
	}
}
